﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Mysqlx;
using Newtonsoft.Json;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using ZH.Infrastructure.Attr;
using ZH.Infrastructure.Dto.Request.Login;
using ZH.Infrastructure.Dto.Request.Tech;
using ZH.Infrastructure.Dto.Request.Yuyue;
using ZH.Infrastructure.Dto.Response;
using ZH.Infrastructure.Dto.Response.admin;
using ZH.Infrastructure.Dto.Response.Article;
using ZH.Infrastructure.Dto.Response.Me;
using ZH.Infrastructure.Dto.Response.Tech;
using ZH.Infrastructure.Dto.Response.Yuyue;
using ZH.Infrastructure.EFCore.MySql;
using ZH.Infrastructure.IOC;
using ZH.Infrastructure.Redis;
using ZH.Infrastructure.Tools;
using ZH.Service.Interface;

namespace ZH.Service.Instance
{
    [Inject_]
    public class Me_Service : Base_Service, I_Me_Service
    {
        private readonly Mobile_IOC _mobile_IOC;
        public readonly Tech_IOC _tech_IOC;
        private readonly IConfiguration _configuration;
        public Me_Service(Mobile_IOC mobile_IOC, IConfiguration configuration, Tech_IOC tech_IOC)
        {
            _mobile_IOC = mobile_IOC;
            _configuration = configuration;
            _tech_IOC = tech_IOC;
        }


        public async Task<Api_Response_Dto> Get_FWcollect(User_Response_Dto? dto, string uid )
        {

            var fw = await _mobile_IOC._fwcollect_EFCore.QueryAll(d => d.FMid == uid).ToArrayAsync();
            var tech = _tech_IOC._tech_Service.QueryAll().ToList();
            var data = from f in fw
                       join t in tech
                       on f.FtechId equals t.TechId
                       select new Tech_Response_Dto
                       {
                           tid =t.TechId,
                           img = t.TechImg,
                           tname = t.TechName,
                           type =t.TechType,
                           tdesc =t.TechDesc,
                           techduration =t.TechDuration.ToString(),
                           price = (decimal)t.TechPrice

                       };
            return Result(1, "ok", data);
        }

        public async Task<Api_Response_Dto>Add_FWcollect(Me_Response_Dto dto, string id)
        {
            var ls = await _mobile_IOC._fwcollect_EFCore.QueryAll(d => d.FMid == id && d.FtechId == dto.ftechid).FirstOrDefaultAsync();
            if (ls == null)
            {
                Fwcollect fw = new Fwcollect();
                fw.FId = Config.GUID();
                fw.FMid = id;
                fw.FtechId = dto.ftechid;
                _mobile_IOC._fwcollect_EFCore.Add(fw);
                var result = _mobile_IOC._fwcollect_EFCore.SaveChanges();

                return Result(result, "收藏成功");
            }
            else {
                _mobile_IOC._fwcollect_EFCore.Delete(ls);
                var result = _mobile_IOC._fwcollect_EFCore.SaveChanges();

                return Result(result, "取消成功");
            }
        }

       
        public async Task<Api_Response_Dto>Del_FWcollect(Me_Response_Dto dto, string id)
        { 
            var fw = await _mobile_IOC._fwcollect_EFCore.QueryAll(d=> d.FId == id).FirstOrDefaultAsync();
            if (fw == null) {
                return Result(0, "未找到");
            }
            _mobile_IOC._fwcollect_EFCore.Delete( fw );
            var result = _mobile_IOC._fwcollect_EFCore.SaveChanges();
            return Result(result, "删除成功");
        }

        public async Task<Api_Response_Dto> Get_sc(string tid, string? uid)
        {
            var ls = await _mobile_IOC._fwcollect_EFCore.QueryAll(d => d.FMid == uid && d.FtechId == tid).FirstOrDefaultAsync();
            if (ls == null)
            {
                return Result(1, "未收藏");
            }
            else {

                return Result(1, "已收藏");
            }
        }
    }
}

